package com.et114.core.control;


import javax.servlet.ServletContext;


import org.apache.struts.action.ActionServlet;


//import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
//import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContextException;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils ;
//import org.springframework.web.struts.ActionServletAwareProcessor;
import org.springframework.web.struts.ContextLoaderPlugIn;

import com.et114.core.common.AppContext;
import com.et114.core.common.CONST;


/**
 * @author guanhw
 */
public class StrutsSpringIOC extends ContextLoaderPlugIn {
	private ServletContext applic = null;

	public WebApplicationContext createWebApplicationContext( WebApplicationContext parent ) throws BeansException {
		logger.info( "*****init spring config************" ) ;
		if ( !ConfigurableWebApplicationContext.class .isAssignableFrom( getContextClass() ) ) {
			throw new ApplicationContextException(
					"Fatal initialization error in servlet with name '"
							+ this.getActionServlet().getServletName()
							+ "': custom WebApplicationContext class ["
							+ getContextClass().getName()
							+ "] is not of type ConfigurableWebApplicationContext");
		}

		//ConfigurableWebApplicationContext wac = ( ConfigurableWebApplicationContext ) BeanUtils.instantiateClass( getContextClass() );
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext ( this.getServletContext ( ) );
		/*
		wac.setParent( parent );
		wac.setServletContext( this.getActionServlet().getServletContext() );
		wac.setNamespace( getNamespace() );
		if ( getContextConfigLocation() != null ) {
			wac.setConfigLocations( new String[] { getContextConfigLocation() } );
		}
		else {
			wac.setConfigLocations( getSpringConfigLocations()  );
		}
		
	*/
		final ActionServlet actionServlet = super.getActionServlet();
		/*
		wac.addBeanFactoryPostProcessor( new BeanFactoryPostProcessor() {
			public void postProcessBeanFactory (
					ConfigurableListableBeanFactory beanFactory ) {
				beanFactory.addBeanPostProcessor( new ActionServletAwareProcessor( actionServlet ) );
			}
		} );
		
		wac.refresh();
		 */
		AppContext.setSpringBeans( wac  ) ;
		
		Object attr = actionServlet.getServletContext().getAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		if ( attr == null )
			actionServlet.getServletContext().setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE , wac );

		logger.info( "*****init spring config end**************************"  ) ;
		return wac;
	}

	

	public String[] getSpringConfigLocations () {
		applic = getActionServlet().getServletContext() ;
		String ccl = applic.getInitParameter( "contextConfigLocation" ) ;
		if ( ccl == null ) {
			return new String[] { CONST.DEFAULT_SPRING_CONFIG_LOC  } ;
		}
		return  new String[] { ccl  } ;
	}

}
